﻿using System;
using System.IO;
using System.Linq;
using System.Web.Security;

/// <summary>
/// Summary description for PersonBLL
/// </summary>
public class PersonBLL
{
    private static PersonBLL instance;
    private DateiaftenDataContext db;

	private PersonBLL()
	{
		db = new DateiaftenDataContext();
	}

    public static PersonBLL Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new PersonBLL();
            }
            return instance;
        }
    }

    public aspnet_User getCurrentUser(MembershipUser user)
    {
        return db.aspnet_Users.SingleOrDefault(x => x.UserName == user.UserName);
    }

    public int returnAge(DateTime? datetime)
    {
        DateTime bday = datetime.Value;
        DateTime today = DateTime.Today;
        int age = today.Year - bday.Year;
        if (bday > today.AddYears(-age)) age--;
        return age;
    }

    public DateTime? returnBirthYear(int age)
    {
        DateTime today = DateTime.Today;
        DateTime? returnDate = new DateTime(today.Year - age, today.Month, today.Day);
        return returnDate;
    }

    public string setProfilePicture(string username, string serverMapPath)
    {
        if (File.Exists(serverMapPath + username + ".png"))
        {
            return "../images/ProfilePictures/" + username + ".png";
        }
        else
        {
            return "../images/ProfilePictures/" + "default.png";
        }
    }

    public Gender FindGenderById(int id)
    {
        return db.Genders.SingleOrDefault(x => x.genderId == id);
    }
}